<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h4>质数判断器</h4>
    <input type="text" id="num1" placeholder="请输入一个数" />
    <button id="btn">判断</button>
    <input type="text" id="rea" placeholder="结果是" />
    <script>
      function calc(a) {
        var flag = true;
        for (var i = 2; i <= a / 2; ++i) {
          if (a % i == 0) {
            flag = false;
            break;
          }
        }
        var res1 = "是质数";
        var res2 = "不是质数";
        if (flag == true) return res1;
        else return res2;
      }
      var numEle = document.getElementById("num1");

      btn.onclick = function () {
        var a = parseInt(numEle.value);
        rea.value = calc(a);
      };
    </script>
  </body>
</html>
